package com.three.iot.channel.impl.chinamobile;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.codec.binary.Base64;

/**
 * @author luiz
 * @Title: PubTest
 * @ProjectName iot-server
 * @Description: TODO
 * @date 2019-05-07 13:16
 */
public class PubTest {
    public static void main(String[] args) {
        // 阿里云accessKey
        String accessKey = "LTAIM4zSiZd7rMzu";
        // 阿里云accessSecret
        String accessSecret = "LiKB95DldnjMSGhzrUIcFEqmysb864";
        try {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        //初始化SDK客户端
        DefaultAcsClient client = new DefaultAcsClient(profile);

        PubRequest request = new PubRequest();
        request.setProductKey("a1BIyMhGr5p");
        request.setMessageContent(Base64.encodeBase64String("hello world".getBytes()));
        //request.setTopicFullName("/productKey/deviceName/get");
        request.setTopicFullName("/sys/a1BIyMhGr5p/2019-001/thing/event/property/post");
        //目前支持QoS0和QoS1
        request.setQos(0);
        try {
            PubResponse response = client.getAcsResponse(request);
            System.err.println(response.getSuccess());
            System.err.println(response.getErrorMessage());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
